#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
} node;

void push(node** tail, int val) {
    node* newNode = (node*)malloc(sizeof(node));
    newNode->data = val;
    newNode->next = NULL;
    (*tail)->next = newNode;
    *tail = newNode;
}
node* mergeList(node* head1, node* head2) {
    node *p1 = head1, *p2 = head2;
    node* tail = (node*)malloc(sizeof(node));
    node* head = tail;
    while (p1 != NULL || p2 != NULL) {
        if (p1 != NULL) {
            push(&tail, p1->data);
            p1 = p1->next;
        }
        if (p2 != NULL) {
            push(&tail, p2->data);
            p2 = p2->next;
        }
    }
    return head->next;
}